package fi.piet.ec2.employee;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;

import fi.piet.ec2.department.Department;
import fi.piet.ec2.service.EmployeeService;

public class EmployeeForm extends Form {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@SpringBean
	public EmployeeService employeeService;
	
	public EmployeeForm(String id, Employee employee, Collection<Department> departmentList) {
		super(id, new CompoundPropertyModel(employee));
		
		DropDownChoice choice=new DropDownChoice("departmentDCC", new PropertyModel(employee, "department"), new ArrayList<Department>(departmentList));
		add(choice);
		
		TextField firstname = new TextField("firstname");
		firstname.setRequired(true);
		firstname.add(StringValidator.maximumLength(24));
		firstname.add(StringValidator.minimumLength(3));
        
        FormComponentFeedbackBorder firstnameFeedback = new FormComponentFeedbackBorder("firstnameFeedback");
        add(firstnameFeedback);
        firstnameFeedback.add(firstname);

		TextField lastname = new TextField("lastname");
		lastname.setRequired(true);
		lastname.add(StringValidator.maximumLength(24));
		lastname.add(StringValidator.minimumLength(3));
        
        FormComponentFeedbackBorder lastnameFeedback = new FormComponentFeedbackBorder("lastnameFeedback");
        add(lastnameFeedback);
        lastnameFeedback.add(lastname);
        
	}
	
	public void onSubmit() {
		Employee emp = (Employee)getModelObject();
		employeeService.storeEmployee(emp);
		setResponsePage(EmployeePage.class);
	}

}
